<template>
  <div class="tktk_container">
    <!--菜单表格-->
    <div class="tktk_btn"><el-button type="primary" size="mini" @click="handleAdd" icon="el-icon-plus">新增分类</el-button>
    </div>
    <el-table v-loading="loading" :data="catalogList" row-key="id">
      <el-table-column type="index" label="序号" width="80" align="center"></el-table-column>
      <el-table-column prop="name" label="分类名称" :show-overflow-tooltip="true" align="center"></el-table-column>
      <el-table-column prop="typeUrl" label="分类图片" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <el-image style="height: 40px; border-radius: 50%;" :src="scope.row.typeUrl"
            :preview-src-list="[scope.row.typeUrl]"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="sortNum" label="排序" align="center"></el-table-column>
      <el-table-column prop="enableFlag" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enableFlag == '1'" type="success">正常</el-tag>
          <el-tag v-else type="danger">停用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="180" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="box-center">
      <el-pagination @current-change="handleCurrentChange" :current-page.sync="params.pageNum"
        layout="prev, pager, next" :page-size="params.pageSize" :total="total">
      </el-pagination>
    </div>

    <!--新增编辑表格-->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="submitForm" :model="form" :rules="rules" label-width="100px" size="small">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="分类图片" prop="typeUrl">
          <div class="upload-goods-accept">建议上传尺寸: 60 * 60</div>
          <div class="upload-goods">
            <el-upload class="avatar-uploader" :action="actionUrl" :headers="headers" :on-success="changeUpdate"
              :show-file-list="false">
              <img v-if="form.typeUrl" :src="form.typeUrl" class="avatar_upload">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="显示排序" prop="sortNum">
          <el-input-number v-model="form.sortNum" :min="0"></el-input-number>
        </el-form-item>

        <el-form-item prop="enableFlag" label="显示状态">
          <div class="radio-group">
            <label><input type="radio" name="enableFlag" value="1" v-model="form.enableFlag" /> 正常</label>
            <label><input type="radio" name="enableFlag" value="0" v-model="form.enableFlag" /> 停用</label>
          </div>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm('submitForm')">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { classifyList, addClassify, updateClassify, deleteClassify } from '@/api/tktk/classify'
export default {
  name: 'classifyList',
  data() {
    return {
      actionUrl: '/api/cargo/file/upload',
      headers: {
        Authorization: this.$store.getters.token
      },
      loading: false,
      open: false,
      title: '新增分类',
      total: 0,
      params: {
        pageNum: 1,
        pageSize: 10
      },
      catalogList: [],
      form: {
        id: '',
        name: '',
        typeUrl: '',
        sortNum: 0,
        enableFlag: '1'
      },
      rules: {
        name: [
          { required: true, message: '分类名称不能为空', trigger: 'blur' }
        ],
        sortNum: [
          { required: true, message: '分类顺序不能为空', trigger: 'blur' }
        ],
        typeUrl: [
          { required: true, message: '请上传分类图片', trigger: 'blur' }
        ],
        enableFlag: [
          { required: true, message: '请选择显示状态', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleUpdate(row) {
      const { id, name, typeUrl, sortNum, enableFlag } = row
      console.log(row, '1111')
      this.open = true
      this.form.id = id
      this.form.name = name
      this.form.typeUrl = typeUrl
      this.form.sortNum = sortNum
      this.form.enableFlag = enableFlag
    },

    changeUpdate(file, fileList) {
      if (file.code == 200) {
        this.form.typeUrl = file.url
      } else {
        this.form.typeUrl = ''
        this.$message.error(file.msg)
      }
    },

    handleAdd() {
      this.open = true
      this.form.id = ''
      this.form.name = ''
      this.form.typeUrl = ''
      this.form.sortNum = 0
      this.form.enableFlag = '1'
    },
    handleDelete(row) {
      const _this = this
      this.$confirm('确定要删除该分类吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteClassify({ id: row.id }).then(res => {
          if (res.code === 200) {
            _this.$message.success('操作成功')
            _this.getCatalogList()
          } else {
            _this.$message.error(res.msg)
          }
        })
      })
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            updateClassify(this.form).then(res => {
              if (res.code === 200) {
                this.$message.success('操作成功')
                this.getCatalogList()
                this.open = false
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            // 新增
            addClassify(this.form).then(res => {
              if (res.code === 200) {
                this.$message.success('操作成功')
                this.getCatalogList()
                this.open = false
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        }
      });
    },
    cancel() {
      this.open = false
    },
    handleCurrentChange(page) {
      this.params.pageNum = page
      this.getCatalogList()
    },
    getCatalogList() {
      classifyList(this.params).then(res => {
        if (res.code === 200) {
          this.catalogList = res.rows || []
          this.total = res.total || 0
        }
      })
    }
  },

  mounted() {
    this.getCatalogList()
  }
}
</script>

<style lang="scss" scoped>
.radio-group {
  display: flex;
  align-items: center;

  label {
    margin: 0 10px;
    cursor: pointer;
  }
}

.upload-goods {
  .el-upload--picture-card {
    width: 80px;
    height: 80px;
    line-height: 78px;

    i {
      font-size: 20px;
    }
  }

  .el-upload-list__item {
    width: 80px;
    height: 80px;
  }

  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 100px;
    height: 100px;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .avatar-uploader:hover {
    border-color: #409eff;
  }
}
</style>
